//
//  HttpProvider.m
//  HYSmileiOS
//
//  Created by alekye on 16/4/26.
//  Copyright © 2016年 heyou. All rights reserved.
//

#import "HttpProvider.h"
#import "NetManager.h"
#import "heyou.h"
#import "ResponseModel.h"
#import "UserManager.h"
#import "HYNotifications.h"
#import "CommentRequestModel.h"

#define K_SERVER_ADDRESS    @"https://httpbin.org/get"
#define K_RESCODE_OK    200

@implementation HttpProvider

/**
 *  根据用户摘要信息获取详情（测试接口）
 *
 *  @param brief    摘要
 *  @param _success 成功回调
 *  @param _failure 失败回调
 */
+ (void)getUserDetailInfo:(UserInfoBrief*)brief
                  success:(void (^)(UserInfoDetail* detail))_success
                  failure:(void (^)(NSError *error))_failure {
    NSDictionary *params = [brief mj_keyValues];
    [[NetManager sharedInstance] GET:K_SERVER_ADDRESS parameters:params success:^(NSDictionary *responseObject) {
        UserInfoDetail *tDetail = nil;
        if (responseObject && [responseObject isKindOfClass:[NSDictionary class]]) {
            tDetail = [UserInfoDetail mj_objectWithKeyValues:[responseObject objectForKey:@"args"]];
        } else {
            NSLog(@"================= responseObject === error!");
        }
        if (_success) {
            _success(tDetail);
        }
    } failure:^(NSError *error) {
        NSLog(@"get user detail failure!");
    }];
}

+ (void)_notifyUserNotLogin {
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter postNotificationName:USER_NOT_LOGIN object:nil];
}

/**
 *  评论
 *
 *  @param cm       评论内容
 *  @param model    评论的微博
 *  @param _success 成功回调
 *  @param _failure 失败回调
 */
+ (void)comment:(CommentModel*)cm
        article:(ArticleModel*)model
        success:(void (^)(CommentModel* ncm))_success
        failure:(void (^)(NSError *error))_failure {
    if ([[UserManager sharedInstance] isUserLogin]) {
        UserInfoDetail *user = [[UserManager sharedInstance] getCurrentUser];
        CommentRequestModel *crm = [CommentRequestModel new];
        crm.uid = user.uid;
        crm.wbid = model.wbid;
        crm.content = cm.content;
        NSMutableDictionary *params = [crm mj_keyValues];

        [[NetManager sharedInstance] GET:K_SERVER_ADDRESS parameters:params success:^(NSDictionary *responseDic) {
            CommentModel *commentM = nil;
            if (responseDic && [responseDic isKindOfClass:[NSDictionary class]]) {
                ResponseModel *resm = [ResponseModel mj_objectWithKeyValues:responseDic];
                if (resm.rescode == K_RESCODE_OK) {
                    commentM = [CommentModel mj_objectWithKeyValues:resm.data];
                } else {
                    NSLog(@"================= rescode === error : %ld", resm.rescode);
                }
            } else {
                NSLog(@"================= responseDic === error!");
            }
            if (_success) {
                _success(commentM);
            }
        } failure:^(NSError *error) {
            if (_failure) {
                _failure(error);
            }
        }];
        
    } else {
        [self _notifyUserNotLogin];
    }
}


@end
